package com.ghost.alibabaghost.service.projects;

import com.ghost.alibabaghost.api.model.projects.ProjectModel;
import com.ghost.alibabaghost.api.service.projects.ProjectService;
import com.ghost.alibabaghost.dao.dataobject.projects.ProjectDO;
import com.ghost.alibabaghost.dao.mapper.projects.ProjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yaolisheng
 * @version 1.0
 * @date 2022/1/18 11:36
 */
@Service("projectService")
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectMapper projectMapper;

    @Override
    public int insert(ProjectModel record) {
        ProjectDO projectDO = new ProjectDO();
        BeanUtils.copyProperties(record, projectDO);
        return projectMapper.insert(projectDO);
    }

    @Override
    public int insertSelective(ProjectModel record) {
        ProjectDO projectDO = new ProjectDO();
        BeanUtils.copyProperties(record, projectDO);
        return projectMapper.insert(projectDO);
    }

    @Override
    public int updateSelective(ProjectModel record) {
        ProjectDO projectDO = new ProjectDO();
        BeanUtils.copyProperties(record, projectDO);
        return projectMapper.insert(projectDO);
    }

    @Override
    public ProjectModel queryById(Integer id) {
        ProjectDO projectDO = projectMapper.queryById(id);
        ProjectModel projectModel = new ProjectModel();
        BeanUtils.copyProperties(projectDO, projectModel);
        return projectModel;
    }

    @Override
    public List<ProjectModel> querySelective(ProjectModel record) {
        ProjectDO projectDO = new ProjectDO();
        BeanUtils.copyProperties(record, projectDO);
        List<ProjectDO> projectDOS = projectMapper.querySelective(projectDO);
        return projectDOS.stream().map(project -> {
            ProjectModel projectModel = new ProjectModel();
            BeanUtils.copyProperties(project, projectModel);
            return projectModel;
        }).collect(Collectors.toList());
    }

    @Override
    public int delete(Integer id) {
        return projectMapper.delete(id);
    }
}
